package server.com.acme.bankapp.domain.bank;

import java.io.Serializable;

import server.com.acme.exceptions.NotEnoughFundsException;
import server.com.acme.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount implements Account,
		Serializable {

	private static final long serialVersionUID = 1L;
	private final static String s0 = "CheckingAccount: balance = ";
	private final static String s1 = ", overdraft = ";
	private final static String s2 = "Amount can not be negative";

	private double overdraft;

	public CheckingAccount(final int id, final double amount,
			final double overdraft) {
		super(amount);
		this.id = id;
		this.overdraft = overdraft;
	}

	@Override
	public void deposit(final double amount) {
		setBalance(getBalance() + amount);
	}

	@Override
	public void withdraw(final double amount)
			throws OverDraftLimitExceededException, NotEnoughFundsException {
		if (amount < 0) {
			throw new IllegalArgumentException(s2);
		}

		if (this.balance > amount) {
			this.balance -= amount;
			return;
		}

		double overdraftNeeded = amount - this.balance;

		if (overdraft < overdraftNeeded) {
			throw new OverDraftLimitExceededException(amount, this);
		} else {
			this.balance = 0.0;
			overdraft -= overdraftNeeded;
			throw new NotEnoughFundsException();
		}

	}

	public double getOverDraft() {
		return this.overdraft;
	}

	@Override
	public double maximumAmountToWithdraw() {
		return getBalance() + this.overdraft;
	}

	@Override
	public String toString() {
		return s0 + balance + s1 + overdraft;
	}

}
